# GPL3 or any later version

import os
import gi
import threading
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GLib

from modules import wallet
from modules import totals
from modules import transactions
from modules import items
from modules import merchants
from modules import ui
from modules import settings

def show_main_window(the_wallet):

    # Shows the main window
    
    win = Gtk.Window()
    win.wallet = the_wallet
    win.connect("destroy", Gtk.main_quit) 
    win.set_title("J.Y.Transactions")         
    win.set_default_icon_from_file("icon_small.png") 
    win.set_size_request(400, 400)
    win.maximize()

    box = Gtk.VBox()
    win.add(box)

    pannel = Gtk.HeaderBar()
    pannel.set_show_close_button(True)
    win.set_titlebar(pannel) 

    # Adding button
    add_button = Gtk.Button()
    add_button.set_relief(Gtk.ReliefStyle.NONE)
    add_button.add(ui.icon(win, "list-add"))
    add_button.set_tooltip_text("Add Transaction")
    pannel.pack_start(add_button)

    def on_add(w):
        transactions.add(win)
    add_button.connect("clicked", on_add)

    # Folder button
    folder_button = Gtk.Button()
    folder_button.set_relief(Gtk.ReliefStyle.NONE)
    folder_button.add(ui.icon(win, "folder"))
    folder_button.set_tooltip_text("Files")
    pannel.pack_end(folder_button)


    def on_folder(w):
        folder = wallet.save_folder()
        os.system("xdg-open "+folder)

    folder_button.connect("clicked", on_folder)

    

    # Settings button
    folder_button = Gtk.Button()
    folder_button.set_relief(Gtk.ReliefStyle.NONE)
    folder_button.add(ui.icon(win, "preferences-system"))
    folder_button.set_tooltip_text("Settings")
    pannel.pack_end(folder_button)

    def on_settings(w):
        settings.dialog(win)

    folder_button.connect("clicked", on_settings)


    
    # Notebook
    notebook = Gtk.Notebook()
    notebook.set_scrollable(True)
    box.pack_start(notebook, 1,1,1)

    # Totals
    
    win.totals_box = Gtk.VBox()
    totals.update_totals(win)
    detailsbox = Gtk.HBox()
    detailsbox.pack_start(Gtk.Label("  Totals  "), True, True, True)
    detailsbox.show_all()
    notebook.append_page(win.totals_box, detailsbox)

    # Transactions

    scrl = Gtk.ScrolledWindow()
    win.transactions_box = Gtk.VBox()
    scrl.add(win.transactions_box)
    transactions.update_transactions(win)
    detailsbox = Gtk.HBox()
    detailsbox.pack_start(Gtk.Label("  Transactions  "), True, True, True)
    detailsbox.show_all()
    notebook.append_page(scrl, detailsbox)
    
    # Items

    def on_item(w, win, item):

        items.editor(win, item)
        items.refresh_items(*refreshpack)
        
    win.items_box = Gtk.VBox()
    refreshpack = items.select_widget(win, win.items_box, on_item)
    detailsbox = Gtk.HBox()
    detailsbox.pack_start(Gtk.Label("  Items  "), True, True, True)
    detailsbox.show_all()
    notebook.append_page(win.items_box, detailsbox)


    # Merchants

    def on_merchant(w, win, item):

        merchants.editor(win, item)
        merchants.refresh_items(*merchants_refreshpack)
        
    win.merchants_box = Gtk.VBox()
    merchants_refreshpack = merchants.select_widget(win, win.merchants_box, on_merchant)
    detailsbox = Gtk.HBox()
    detailsbox.pack_start(Gtk.Label("  Merchants  "), True, True, True)
    detailsbox.show_all()
    notebook.append_page(win.merchants_box, detailsbox)
    
    pannel.pack_start(Gtk.Label(the_wallet))
    
    win.show_all()
    Gtk.main()
